<template>
  <el-card :style="!isPc?'paddingLeft:0;':''" :class="{'mobile-card': !isPc}" class="user-info-card">
    <div :class="{'mobile-user-info': showMobilePage}" class="user-info-container">
      <div class="avatar" @click="toAccountNum">
        <img :src="userData.avatar || userAvatar" alt="avatar" >
      </div>
      <div class="user-info-content">
        <div class="user-name info-cell">
          <ToolTip :content="`${userData.greetings || '您'}好，${userData.userName || '欢迎登录共创'}！`"/>
        </div>
        <div :class="{'flex mobile-box flex-nowrap': !isPc}">
          <div class="info-cell">
            工号：<span style="color: #333">{{ userData.jobNumber || '--' }}</span>
          </div>
          <div class="flex-center info-cell">
            <span>部门：</span>
            <ToolTip :content="userData._departmentName" class="dept-name" />
          </div>
        </div>
      </div>
    </div>
    <div class="middle-line" />
    <ul :class="{'flex-row mobile-box flex-50': !isPc}" class="card-bottom-box">
      <li class="info-cell">在飞博共创工作了<span class="card-bottom-number">{{ userData.workDays | isNumberEmpty }}</span>天</li>
      <li class="info-cell">共完成<span class="card-bottom-number">{{ userData.approvalNum | isNumberEmpty }}</span>次审批</li>
    </ul>
  </el-card>
</template>

<script>
import { getDataInfo } from '@/api/home';
import { isNumberEmpty } from '@/filters/number';
import userAvatar from '@/assets/user-avatar.png';
import { mapState } from 'vuex';

export default {
  name: 'UserInfoCard',
  filters: { isNumberEmpty },
  props: {
    isPc: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      userAvatar,
      userData: {}
    };
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo,
      'showMobilePage': state => state.app.showMobilePage
    })
  },
  created() {
    this.queryUserCardInfo();
  },
  methods: {
    queryUserCardInfo() {
      getDataInfo().then(res => {
        this.userData = {
          greetings: res.data.greetings,
          ...this.userInfo
        };
        if (this.userData.deptName && this.userData.deptName.length) {
          this.userData._departmentName = this.userData.deptName.map(i => i.name).join('/');
        }
      }).catch(_ => false);
    },
    toAccountNum() {
      const url = this.$router.resolve({
        name: 'userDetail',
        params: { userId: this.userInfo.id }
      });
      window.open(url.href, '_blank');
    }
  }
};
</script>

<style lang="scss" scoped>
.user-info-card {
  width: 100%;
  height: 100%;
  border: none;
  padding-left: 12px;
  & ::v-deep {
    .el-card__body {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      height: 100%;
      padding: 0;
      .avatar {
        width: 84px;
        height: 84px;
        border-radius: 50%;
        margin-right: 16px;
        background: #fff;
        cursor: pointer;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 2px 4px 6px 0 rgba(221,225,234,0.85);;
        img {
          width: 80px;
          height: 80px;
          border-radius: 50%;
        }
      }
    }
  }
  .user-info-container {
    width: 100%;
    flex: 3;
    display: flex;
    align-items: center;
    position: relative;
    .user-info-content {
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      font-size: 14px;
      color: #999;
      width: calc(100% - 100px);
      .user-name {
        font-size: 18px;
        font-weight: bold;
        color: #333;
      }
    }
  }
  .mobile-user-info {
    .avatar {
      margin-right: 10px;
    }
  }
  .info-cell {
    white-space: nowrap;
    .dept-name {
      color: #333;
      width: calc(100% - 42px);
    }
  }
  .middle-line {
    min-height: 1px;
    width: 100%;
    margin: 6px 0;
    background-color: #EBEEF5;
  }
  .card-bottom-box {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    font-size: 14px;
    margin: 0;
    padding-left: 18px;
    color: #333;
    .info-cell::marker {
      color: #ddd;
    }
    .card-bottom-number {
      border-radius: 999px;
      background-color: #406eff;
      color: #fff;
      padding: 0 7px;
      height: 16px;
      line-height: 16px;
      margin: 0 4px;
      display: inline-block;
    }
  }
  .flex-row {
    flex-direction: row;
    justify-content: flex-start;
  }
  .mobile-box {
    .info-cell {
      flex-wrap: wrap;
      width: 50%;
    }
  }
  .flex-nowrap {
    .info-cell:nth-of-type(2) {
      flex-wrap: nowrap;
    }
  }
  .flex-50 {
    padding-left: 0;
    list-style: none;
    .info-cell:nth-of-type(1), .info-cell:nth-of-type(2) {
      &::before {
        content: '';
        display: inline-block;
        width: 4px;
        height: 4px;
        position: relative;
        top: -1px;
        margin-right: 4px;
        border-radius: 50%;
        background: #ddd;
      }
    }
    .info-cell:nth-of-type(2) {
      margin-left: 6px;
    }
  }
}
.mobile-card {
  & ::v-deep {
    .el-card__body {
      .avatar {
        width: 46px;
        height: 46px;
        img {
          width: 46px;
          height: 46px;
          border: 2px solid #ffffff;
          border-radius: 50%;
          box-shadow: 0px 3px 6px 0px #dddddd;
        }
      }
    }
  }
  .user-info-container {
    .user-info-content {
      .info-cell {
        font-size: 12px;
      }
      .user-name {
        font-size: 14px;
      }
    }
  }
  .card-bottom-box {
    .info-cell {
      font-size: 12px;
    }
  }
}
</style>
